﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsCatchParseContent
    {
        private readonly KeRui.DAL.CmsCatchParseContent dal = new KeRui.DAL.CmsCatchParseContent();

        public int Add(KeRui.Model.CmsCatchParseContent model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsCatchParseContent> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsCatchParseContent> list = new List<KeRui.Model.CmsCatchParseContent>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsCatchParseContent item = new KeRui.Model.CmsCatchParseContent();
                    if (dt.Rows[i]["ParseContentID"].ToString() != "")
                    {
                        item.ParseContentID = int.Parse(dt.Rows[i]["ParseContentID"].ToString());
                    }
                    item.ParseContentName = dt.Rows[i]["ParseContentName"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        item.IsUsing = int.Parse(dt.Rows[i]["IsUsing"].ToString());
                    }
                    item.ContentEncoding = dt.Rows[i]["ContentEncoding"].ToString();
                    item.ContentParseBegin = dt.Rows[i]["ContentParseBegin"].ToString();
                    item.ContentParseEnd = dt.Rows[i]["ContentParseEnd"].ToString();
                    item.ContentParseTags = dt.Rows[i]["ContentParseTags"].ToString();
                    item.ContentParseAttrs = dt.Rows[i]["ContentParseAttrs"].ToString();
                    item.ContentParseRule = dt.Rows[i]["ContentParseRule"].ToString();
                    item.ContentPageParseRule = dt.Rows[i]["ContentPageParseRule"].ToString();
                    item.BlackList = dt.Rows[i]["BlackList"].ToString();
                    item.WhileList = dt.Rows[i]["WhileList"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["CreateUserID"].ToString() != "")
                    {
                        item.CreateUserID = int.Parse(dt.Rows[i]["CreateUserID"].ToString());
                    }
                    item.CreateUserName = dt.Rows[i]["CreateUserName"].ToString();
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateUserID"].ToString() != "")
                    {
                        item.UpdateUserID = int.Parse(dt.Rows[i]["UpdateUserID"].ToString());
                    }
                    item.UpdateUserName = dt.Rows[i]["UpdateUserName"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ParseContentID)
        {
            this.dal.Delete(ParseContentID);
        }

        public bool Exists(int ParseContentID)
        {
            return this.dal.Exists(ParseContentID);
        }

        public List<KeRui.Model.CmsCatchParseContent> GetList(string strCondition)
        {
            string strWhere = "";
            string strOrder = "";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParseContent", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseContent> GetList(string where, string order, int size, int index)
        {
            string strWhere = where;
            string strOrder = order;
            int pageSize = size;
            int pageIndex = index;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParseContent", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseContent> GetListByCache(string strCondition)
        {
            string cacheKey = "CmsCatchParseContentList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsCatchParseContent>) cache;
        }

        public KeRui.Model.CmsCatchParseContent GetModel(int ParseContentID)
        {
            return this.dal.GetModel(ParseContentID);
        }

        public KeRui.Model.CmsCatchParseContent GetModelByCache(int ParseContentID)
        {
            string cacheKey = "CmsCatchParseContentModel-" + ParseContentID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ParseContentID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsCatchParseContent) cache;
        }

        public int Update(KeRui.Model.CmsCatchParseContent model)
        {
            return this.dal.Update(model);
        }
    }
}

